package api

import (
	"github.com/gin-gonic/gin"
	"service"
	"pojo/form"
	"constant"
	"pojo/vo"
	"net/http"
)

type OrderApi struct {
	service *service.OrderService
}

// 创建OrderApi
func NewOrderApi(service *service.OrderService) *OrderApi {

	return &OrderApi{
		service: service,
	}
}


// 新增管理员信息
func (this *OrderApi) Insert(ctx *gin.Context) {

	orderForm := &form.OrderForm{}

	var result vo.Base

	err := ctx.Bind(orderForm)

	if err != nil { // 绑定参数出现错误
		result = constant.ARGUMENTS_BIND_ERROR
		result.Info = err.Error()

		ctx.JSON(http.StatusOK, result)
		return
	}

	// 参数验证
	ok, err := orderForm.Validate()

	if !ok { // 验证不通过
		result = constant.ARGUMENTS_VALIDATE_ERROR
		result.Info = err.Error()
		ctx.JSON(http.StatusOK, result)
		return
	}

	// 验证通过
	err = this.service.InsertOrder(orderForm)
	if err != nil { // 保存失败
		result = constant.SERVICE_ERROR
		result.Info = err.Error()
	} else {
		result = constant.SUCCESS
	}

	ctx.JSON(http.StatusOK, result)
}

// 修改订单信息
func (this *OrderApi) Update(ctx *gin.Context) {

}

// 删除订单信息
func (this *OrderApi) Delete(ctx *gin.Context) {

}
